﻿using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface;
using IntelligentAgriculture.Domain.Interface.SheepFileModelInterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Domain.Impl
{
    /// <summary>
    /// 种母配种仓储实现
    /// </summary>
    public class BreedingInfoRepository : IBreedingInfoRepository
    {
        /// <summary>
        /// 上下文
        /// </summary>
        private readonly AgricultureDbContext _dbContext;
      

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbContext">上下文</param>
        public BreedingInfoRepository(AgricultureDbContext dbContext)
        {
            _dbContext = dbContext;
        }



        /// <summary>
        /// 种母配种显示
        /// </summary>
        /// <returns>返回数据</returns>

        public async Task<List<Breedinginfo>> GetBreedinginfoAsync()
        {
            return _dbContext.Breedinginfos.Where(c => c.Isdel == false).ToList();
        }
        /// <summary>
        /// 种母配置修改批量
        /// </summary>
        /// <param name="breedinginfo"></param>
        /// <returns></returns>
        public async Task<int> UpdBreedinginfos (List<Breedinginfo> breedinginfo)
        {
            _dbContext.Breedinginfos.UpdateRange(breedinginfo);
            return await _dbContext.SaveChangesAsync();
        }
        /// <summary>
        /// 种母配置修改
        /// </summary>
        /// <param name="breedinginfo"></param>
        /// <returns></returns>
        public async Task<int> UpdBreedinginfo(Breedinginfo breedinginfo)
        {
            _dbContext.Breedinginfos.Update(breedinginfo);
            return await _dbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 逻辑删除种母配种表
        /// </summary>
        /// <param name="breedId">主键id</param>
        /// <returns>受影响的行数</returns>
        public async Task<int> DeleteBreedingInfo(int breedId)
        {

            var breedingInfo = _dbContext.Breedinginfos.FirstOrDefault(c => c.BreedId == breedId);
            if (breedingInfo != null)
            {
                breedingInfo.Isdel = true;
                _dbContext.Breedinginfos.Update(breedingInfo);
                await _dbContext.SaveChangesAsync();

                // 删除后更改羊只档案的数据信息
                var sheeps = _dbContext.Sheepfilemodels.Where(c => c.EarTag == breedingInfo.EarTag);
                foreach (var sheep in sheeps)
                {
                    // 根据需要更新羊只档案的字段
                    sheep.ColumnId = breedingInfo.CurrentField;
                    sheep.BuildingId=breedingInfo.CurrentField;
                    _dbContext.Sheepfilemodels.Update(sheep);
                }

                return await _dbContext.SaveChangesAsync();
            }

            return 0; // 如果未找到对应的种母配种记录，返回影响行数为0
        }

        /// <summary>
        /// 批量逻辑删除种母配种表
        /// </summary>
        /// <param name="breedIds">id</param>
        /// <returns>返回数据</returns>
        public async Task<int> DeleteRangeBreedingInfo(IEnumerable<int> breedIds)
        {
            foreach (int item in breedIds)
            {
                var list = _dbContext.Breedinginfos.FirstOrDefault(c => c.BreedId == item);
                if (list != null)
                {
                    list.Isdel = true; // 标记为逻辑删除
                    _dbContext.Breedinginfos.Update(list);

                    // 假设羊只档案表名为 SheepArchives，关联字段为 SheepId
                    var sheepArchive = _dbContext.Sheepfilemodels.FirstOrDefault(s => s.EarTag == list.EarTag);
                    if (sheepArchive != null)
                    {
                        // 根据需要更新羊只档案的字段
                        sheepArchive.ColumnId = list.CurrentField;
                        sheepArchive.BuildingId = list.CurrentBuilding;
                        _dbContext.Sheepfilemodels.Update(sheepArchive);
                    }
                }
            }
            return await _dbContext.SaveChangesAsync();


        }

        /// <summary>
        /// 添加种母配种数据
        /// </summary>
        /// <param name="inputs">实体</param>
        /// <returns>返回受影响的行数</returns>
        public Task<int> AddBreedinginfoAsync(List<Breedinginfo> inputs)
        {

            _dbContext.Breedinginfos.AddRangeAsync(inputs);

            return _dbContext.SaveChangesAsync(); // 异步保存所有更改到数据库
        }


        //public Task<int> UpdateAsync(Breedinginfo breedinginfo)
        //{

        //}



    }
}
